#ifndef _STREAM_ENCODER_H_
#define _STREAM_ENCODER_H_

#include <QtCore>
#include <pthread.h>
#include "def.h"
#include "AudioEncoder.h"
#include "VideoEncoder.h"

class StreamEncoder : public QThread
{
	Q_OBJECT

public:
	StreamEncoder(const int channels, const int sampleRate, const int quality, const int fps, const int vWidth, const int vHeight,
		const int vquality, const int bitrate, const int keyframeshift, const int dropframe, const int fastmode);
	~StreamEncoder(void);

	int encodeHeader();
	int encodeVideo(const unsigned char *data);
	int encodeAudio(const TYPE_SAMPLE_IN **data, const int size);
	int encodeRemain();
	int reset(const int channels, const int sampleRate, const int quality, const int fps, const int vWidth, const int vHeight, 
		const int vquality, const int bitrate, const int keyframeshift, const int dropframe, const int fastmode);

signals:
	void onProcessVideoHeaders(PacketList *packets);
	void onProcessVideoPackets(PacketList *packets);
	void onProcessAudioHeaders(PacketList *packets);
	void onProcessAudioPackets(PacketList *packets);
	void onProcessOggPage(const unsigned char* header, const int header_len, const unsigned char* data, const int size);
#if DEBUG
	void logged(const char*);
#endif

private:
	AudioEncoder*			m_lpAudioEncoder;
	VideoEncoder*			m_lpVideoEncoder;

	ogg_stream_state*		m_lpOggVideoStream;
	ogg_stream_state*		m_lpOggAudioStream;
	ogg_stream_state*		m_lpOggSkeletonStream;

	void					initOggStreams();
	int						encodeAudioPackets(PacketList *packets, int bFlushed = 0);
	int						encodeVideoPackets(PacketList *packets, int bFlushed = 0);

	typedef struct _AudioData {
		TYPE_SAMPLE_IN **data;
		int size;
	} AudioData;

	typedef struct _VideoData {
		unsigned char *data;
	} VideoData;

	int						m_bExit;
	QQueue<VideoData*>		m_queueVideoData;
	QQueue<AudioData*>		m_queueAudioData;
	QMutex					m_queueVideoLock;
	QMutex					m_queueAudioLock;
	virtual void run();
};

#endif // _STREAM_ENCODER_H_
